<?php

class Role extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function create($cond) {
		//if ($this->exist($cond['role'])) return FALSE;
		$cond['status'] = 1;
		$cond['created_at'] = date('Y-m-d h:i:s', mktime());
		$cond['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->insert('role', $cond);
		return $this->db->insert_id();
	}

	function rfilter($data) {
		$msg = array();	
		if (!is_array($data) OR empty($data)) $msg[] = "输入数据错误";
		if (!array_key_exists('role', $data)) $msg[] = '角色名不能为空';
		if (strlen($data['role']) > 32) $msg[] = '角色名不能超过32字节';
		if ($this->exist($data['role'])) $msg[] = '角色名已经存在';
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}

	function rufilter($data) {
		$msg = array();	
		if (!is_array($data) OR empty($data)) $msg[] = "输入数据错误";
		if (!array_key_exists('role', $data)) $msg[] = '角色名不能为空';
		if (strlen($data['role']) > 32) $msg[] = '角色名不能超过32字节';
		if (!$this->exist($data['role'])) $msg[] = '角色名不存在';
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}

	function exist($role) {
		$cond = array('role' => $role);
		$this->db->where($cond);
		$query = $this->db->get('role');
		if ($query->num_rows() >= 1) return TRUE;
		return FALSE;	
	}


	function all_role($start = 0, $per_page = 10) {
		$cond = array('status' => 1);	
		$this->db->where($cond); 
		$this->db->order_by('id', 'desc');
		$this->db->limit($per_page, $start);
		$query = $this->db->get('role');
		return $query->result_array();
	}


	function rtotal() {
		$cond = array('status' => 1);
		$this->db->where($cond);
		$query = $this->db->get('role');
		return $query->num_rows();
	}

	function del($id) {
		$data = array('status' => 0);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where('id', $id);
		$this->db->update('role', $data); 
	}

	function find($id) {
		$this->db->where('id', $id); 
		$query = $this->db->get('role');
		return $query->result_array();
	}

	function up($id, $data) {
		unset($data['id']);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where_in('id', $id);
		$this->db->update('role', $data); 
	}

	function module_tree() {
		$cond = array();
		$cond['status'] = 1;
		$this->db->where($cond);
		$query = $this->db->get('module');
		$r = $query->result_array();
		$result = array();
		foreach ($r as $m) {
			if (!array_key_exists($m['module'], $result)) {
				$result[$m['module']] = array('module' => $m['module'], 'module_cname' => $m['module_cname']);
				$result[$m['module']]['atype_1'] = 0 ;
				$result[$m['module']]['atype_2'] = 0 ;
				$result[$m['module']]['sub'][$m['id']] = 
					array('sub_module' => $m['sub_module'], 'sub_module_cname' => $m['sub_module_cname'], 'atype' => $m['atype']);
			} else {
				$result[$m['module']]['sub'][$m['id']] = 
					array('sub_module' => $m['sub_module'], 'sub_module_cname' => $m['sub_module_cname'], 'atype' => $m['atype']);
			}
			$result[$m['module']]['atype_'.$m['atype']]++;
		}
		return $result;
	}

	function priv_filter($data) {
		$msg = array();
		if (!array_key_exists('role_id', $data) OR $data['role_id'] == 0) $msg[] = '角色为空';
		if (!array_key_exists('module_id', $data) OR $data['module_id'] == 0) $msg[] = '功能为空';
		//if (!array_key_exists('node_id', $data) OR $data['node_id'] == 0) $msg[] = '资源为空';
		if ($this->priv_exist($data)) $msg[] = '权限已经存在';
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}

	function add_priv($cond) {
		$cond['status'] = 1;
		$cond['created_at'] = date('Y-m-d h:i:s', mktime());
		$cond['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->insert('privilege', $cond);
		return $this->db->insert_id();
	}

	function priv_exist($cond) {
		$this->db->where(array('role_id' => $cond['role_id'], 'module_id' => $cond['module_id'], 'node_id' => $cond['node_id'], 'status' => 1));
		$query = $this->db->get('privilege');
		if ($query->num_rows() >= 1) return TRUE;
		return FALSE;	
	}

	function del_priv($cond) {
		$cond['status'] = 0;
		$this->db->where(array('role_id' => $cond['role_id'], 'module_id' => $cond['module_id'], 'node_id' => $cond['node_id'], 'status' => 1));
		$this->db->update('privilege', $cond); 
	}

	function fetch_priv($role_id, $atype) {
		$cond = array('role_id' => $role_id, 'status' => 1, 'node_id' => $atype);
		$this->db->where($cond);
		$query = $this->db->get('privilege');
		$r = $query->result_array();
		$result = array();
		foreach ($r as $v) {
			$result[] = $v['module_id'];
		}
		return $result;
	}

}
